WWL.gallery = function ( name )
{			
	//{{{gallery_img = function ( src, alt, title, thumb_src, youtube  )
	var gallery_img = function ( src, alt, title, thumb_src, youtube  )
	{
		var self = this;

		this.src = src;
		this.alt = alt;
		this.title = title;
		this.className =  'gallery_img_img';
		this.youtube = youtube;

		this.thumb_w = 100;
		this.thumb_h = 50;

		this.screen_w = 400;
		this.screen_h = 380;

		if ( ! thumb_src ) 
		{
			this.thumb_src = src; 
		} else {
			this.thumb_src = thumb_src; 
		}

		this.is_youtube = function ()
		{
			if ( ! this.youtube ) return false;
			if ( ! this.youtube.length ) return false;

			return true;
		};

		this.set_thumb_size = function ( w , h )
		{
			self.thumb_w = w;
			self.thumb_h = h;
		};

		this.set_thumb_src =  function ( src )
		{
			self.thumb_src = src;
		};
		
		this.get_img_html = function ( w, h, class_name, cback )
		{
			self.get_img ( w, h, class_name, function ( img )
			{
				var res, dict;
				var s, l = 0, t = 0, img_w, img_h;
				var ratio_img =  img.width/img.height;
				var ratio_cnt = w/h;

				if ( ratio_img > ratio_cnt )
				{
					img_w = w;
					img_h = Math.round ( w / ratio_img );
					t = Math.round ( ( h / 2 ) - ( img_h / 2 ) );
				}else{

					img_w = Math.round ( h * ratio_img );
					img_h = h;
					l = Math.round ( ( w / 2 ) - ( img_w / 2 ) );
				}

				res= '<img class="%(class)s" alt="%(alt)s" style="width: %(w)spx; height: %(h)spx; left: %(left)spx; top: %(top)spx; position:absolute;" title="%(title)s" src="%(src)s">';

				dict = { 'alt': img.alt, 'title' : img.title , 'src' : img.src, 'class' : img.className, 'w' : img_w, 'h' : img_h, 'left' : l, 'top' : t  } ;
						
				s = String.formatDict ( res, dict );
				cback && cback ( s );
			} );
		};
		
		this.get_img = function ( w, h, class_name, cback )
		{
			var img = document.createElement ( 'img' );

			if ( cback ) liwe.events.once ( img, 'load', function () { cback ( img ) } );

			img.alt = self.alt;
			img.title = self.title

			// if ( thumb ) 
			// {
				// img.width = w; // self.thumb_w;
				// img.height = h; //self.thumb_h;
			img.src = self.thumb_src;
			img.className = class_name;
			// };

			img.src  = self.src;
		};	
	};
	//}}}

	var gallery;


	if  ( WWL.get_instance ( "gallery", name ) )
	{
		gallery =  WWL.get_instance ( "gallery", name )
	} else {
		gallery = new WWL ( 'gallery', name );
	}

	// =======================================================
	// PUBLIC METHODS
	// =======================================================

	gallery.clear = function ()
	{
		gallery._items  = [];
		gallery._arrow_box.clear();

		gallery._switch_id = 0;

		gallery._first_run = true;
	};

	//{{{gallery.add_item = function ( src, alt, title, thumb_src, youtube , item )
	gallery.add_item = function ( src, alt, title, thumb_src, youtube, item )
	{
		if ( item instanceof gallery_img ) 
		{
			gallery._items.push ( item );
			return;
		}

		if ( ! src ) return;

		if ( ! thumb_src ) thumb_src = src;

		var _youtube = null;

		if ( youtube ) _youtube = youtube

		item = new gallery_img ( src, alt, title, thumb_src , youtube);
		item.set_thumb_size ( gallery._thumb_size [ 'w' ], gallery._thumb_size [ 'h' ] )

		gallery._items.push ( item );
	};
	//}}}
	//{{{gallery.set_thumb_size = function ( w, h )
	gallery.set_thumb_size = function ( w, h )
	{
		gallery._thumb_size = { w : w , h : h };
	};
	//}}}
	//{{{gallery.set_player_size = function ( w, h )
	gallery.set_player_size = function ( w, h )
	{
		gallery._player_size = { w : w , h : h };
	};
	//}}}
	// {{{ img_next () - Show the next image
	gallery.img_next = function ()
	{
		gallery._del_video ();

		// gallery._display ( ( gallery._curr_pos + 1 ) % gallery._items.count () );
		gallery._arrow_box.sb.move_next ();
	};
	// }}}
	// {{{ img_prev () - Show the prev image
	gallery.img_prev = function ()
	{
		gallery._del_video ();
		gallery._arrow_box.sb.move_prev ();
	};
	// }}}
	
	gallery._del_video = function ()
	{
		$( gallery._elems [ 'video' ], '' );
		$( gallery._elems [ 'footer' ] ).style.display = 'block';
		gallery.thumbs_show ();
	};

	gallery.title_show = function () { gallery._fade_in ( gallery._elems [ 'header' ] ); };
	gallery.title_hide = function () { gallery._fade_out ( gallery._elems [ 'header' ] ); };

	// {{{ title_set ( html ) - Sets the Title text
	gallery.title_set  = function ( title ) 
	{ 
		$( gallery._elems [ 'title' ], title );
	};
	// }}}
	// {{{ hide_all () - Hides all controls
	gallery.hide_all = function ()
	{
		gallery.title_hide ();
		gallery.controls_hide ();
		gallery.thumbs_hide ();

		gallery._controls_shown = false;
	};
	// }}}
	// {{{ show_all () - Shows all controls
	gallery.show_all = function ()
	{
		if ( gallery._controls_shown ) return;

		gallery.title_show ();
		gallery.controls_show ();
		gallery.thumbs_show ();

		gallery._controls_shown = true;
	};
	// }}}
	// {{{ controls_show () - Shows "prev", "next" and "video"
	gallery.controls_show = function () { 
		gallery._fade_in ( gallery._elems [ 'arrleft' ] ); 
		gallery._fade_in ( gallery._elems [ 'arrright' ] ); 

		gallery.video_show ();

	};
	// }}}
	// {{{ controls_hide () - Hides "prev", "next" and "video"
	gallery.controls_hide = function () 
	{
		gallery._fade_out ( gallery._elems [ 'arrleft' ] ); 
		gallery._fade_out ( gallery._elems [ 'arrright' ] ); 

		gallery.video_hide ();
	};
	// }}} 
	// {{{ video_show () - Shows video (if needed)
	gallery.video_show = function () 
	{
		if ( ! gallery._curr_item ) return;
		if ( ! gallery._curr_item.is_youtube () ) return;

		gallery._fade_in ( gallery._elems [ 'player' ] );
	};
	// }}} 
	// {{{ video_hide () - Hides video (if needed)
	gallery.video_hide = function () 
	{
		gallery._fade_out ( gallery._elems [ 'player' ] );
	};
	// }}}
	// {{{ thumbs_show () - Shows thumbnamils section
	gallery.thumbs_show = function () 
	{
		var el = gallery._elems [ 'footer' ];

		if ( gallery._is_ie )
		{
			el.style.bottom = "0";
			el.style.display = "block";
			return;
		}

		liwe.fx.animate ( el, { "bottom" : "0" } );
		gallery._thumbs_shown = true;

	};
	// }}}
	// {{{ thumbs_hide () - Hides thumbnails section
	gallery.thumbs_hide = function () 
	{
		var el = gallery._elems [ 'footer' ];
		var move = "-" + gallery._thumb_size [ 'h' ] + "px";
		if ( gallery._is_ie )
		{
			el.style.bottom = move;
			el.style.display = "none";
			return;
		}

		liwe.fx.animate ( el, { "bottom" : move } );
		gallery._thumbs_shown = false;
	};
	// }}}
	// {{{ render ( [ dest_div ] )
	gallery.render = function ( dest_div )
	{
		if ( ! dest_div ) dest_div = gallery._dest_div;
		gallery._dest_div = $el ( dest_div );
		
		//fix pig: set gallery _size based on dest div. The values will be used by the arrow box.
		var div_size = liwe.dom.get_size ( gallery._dest_div );
		gallery._size = { 'w': div_size [ 0 ], 'h': div_size [ 1 ] };

		var dct = { "_name" : gallery.name };
		var s = String.formatDict ( WWL.gallery.templates [ 'gallery' ], dct );

		$( dest_div, s );

		gallery._elems = {};

		var elems = [ "gallery", "img0", "img1", "header", "title", "arrleft", "arrright", "player", "footer", "arrbox", "fullscreen", "video" ];

		elems.iterate ( function ( n ) 
		{
			gallery._elems [ n ] = $el ( gallery.name + "_" + n );
		} );

		//fix pig: set arrow box based on icon size.
		gallery._elems [ 'arrbox' ].style.height = gallery._thumb_size [ 'h' ] + 'px'; 
		gallery._arr_box_render ();
	};
	// }}}
	//{{{gallery.media_manager = function ( lst_media, dest_div )
	gallery.media_manager = function ( lst_media, dest_div )
	{
		var src = null;
		var data = null;

		gallery.clear ();

		lst_media.iterate ( function ( media )
		{
			data = null;
			src = "/site/media_manager/"+ media [ 'kind' ] +"/full/" + media [ 'id_media' ] + ".jpg";
			src_thumb = "/site/media_manager/"+ media [ 'kind' ] +"/icon/" + media [ 'id_media' ] + ".jpg";
			if ( media [ 'kind' ] == 'youtube' ) data = media [ 'data' ] ;

			gallery.add_item ( src, media [ 'descr' ], media [ 'descr' ], src_thumb, data ); 
		});

		gallery.render ( dest_div );
	};
	//}}}
	// {{{ evt_hover () - Mouse over event
	gallery.evt_hover = function ()
	{
		//console.debug ( "HOVER" );
		if ( gallery._timeout ) clearTimeout ( gallery._timeout );
	
		gallery._timeout = 0;

		gallery.show_all ();
	};
	// }}}
	// {{{ evt_out () - Mouse out event
	gallery.evt_out = function ()
	{
		if ( gallery._timeout ) clearTimeout ( gallery._timeout );

		gallery._timeout = setTimeout ( gallery.hide_all, gallery.SHOW_TIMEOUT );
	};
	// }}}
	// {{{ block_timeout () - Blocks fading out of controls
	gallery.block_timeout = function ()
	{
		if ( gallery._timeout ) clearTimeout ( gallery._timeout );
		gallery._timeout = 0;
	};
	// }}}

	// =======================================================
	// INTERNAL ATTRIBUTES
	// =======================================================
	
	gallery._ie = ! liwe.utils.supports ( 'transition' );
	gallery._thumb_size = { w : 100, h : 50 };
	gallery._player_size = { w : 120, h : 120 };
	gallery._size = { w : 600, h : 180 };
	gallery._switch_id = 1;
	gallery._first_run = true;
	gallery._is_full = false;
	gallery._dest_div = null;
	gallery._img = [ 0, 0 ];
	gallery._items = [];
	gallery._elems = {};
	gallery._is_ie = ! liwe.utils.supports ( "transform" );
	gallery._timeout = 0;
	gallery._curr_pos = 0;
	gallery._curr_item = 0;
	gallery._controls_shown = false;
	gallery._thumbs_shown = true;
	gallery.SHOW_TIMEOUT = 1500;

	if ( ! gallery._arrow_box ) gallery._arrow_box = new WWL.arrowbox ( "gallery-" + name );


	// =======================================================
	// INTERNAL METHODS
	// =======================================================
	// {{{ _display ( pos ) - Shows the 'pos'th image
	gallery._display = function ( pos )
	{
		if ( pos == 0 )
			gallery._fade_out ( gallery._elems [ 'arrleft' ] );
		else 
			gallery._fade_in ( gallery._elems [ 'arrleft' ] );

		if ( pos == ( gallery._items.count()- 1 ) )
			gallery._fade_out ( gallery._elems [ 'arrright' ] );
		else
			gallery._fade_in ( gallery._elems [ 'arrright' ] );


		var item = gallery._items [ pos ];
		var dest = gallery._get_next_dest ();
		var size = liwe.dom.get_size ( gallery._elems [ 'gallery' ] );

		if ( ! item ) return;

		item.get_img_html ( size [ 0 ], size [ 1 ], '', function ( html_img )
		{
			$( gallery._elems [ 'img' + dest ], html_img );

			gallery._switch ();
			gallery._curr_pos = pos;
			gallery._curr_item = item;

			if ( item.is_youtube () )
				gallery.video_show ();
			else
				gallery.video_hide ();

			gallery.title_set ( String.formatDict ( "<b>%(title)s</b><br />%(alt)s", item ) );
		} );
	};
	// }}}
	// {{{ _fade_in ( elem ) - Fades in ``elem``
	gallery._fade_in = function ( elem )
	{		
		if ( gallery._is_ie )
		{
			elem.style.display = 'block';
			return;
		}
		
		liwe.fx.fade_in ( elem );
	};
	// }}}
	// {{{ _fade_out ( elem ) - Fades out ``elem``
	gallery._fade_out = function ( elem )
	{
		if ( gallery._is_ie )
		{
			elem.style.display = 'none';
			return;
		}

		liwe.fx.fade_out ( elem );
	};
	// }}}
	// {{{ _get_next_dest () - Calculates the next image container
	gallery._get_next_dest = function ()
	{
		return 1 - gallery._switch_id;
	};
	// }}}
	// {{{ _switch () - Switches containers
	gallery._switch = function ()
	{
		var curr = gallery._switch_id;	
		var next = gallery._get_next_dest ();

		/*
		liwe.fx.fade_out ( gallery._elems [ 'img' + curr ] ); 
		liwe.fx.fade_in  ( gallery._elems [ 'img' + next ] );
		*/
		gallery._fade_out ( gallery._elems [ 'img' + curr ] );
		gallery._fade_in ( gallery._elems [ 'img' + next ] );

		gallery._switch_id = next;
	};
	// }}}
	// {{{ _arr_box_render () - Init the ArrowBox
	gallery._arr_box_render = function ( pos )
	{
		var count, items;

		gallery._arrow_box.clear ();

		gallery._arrow_box.set_size  ( gallery._size [ 'w' ], gallery._thumb_size [ 'h' ]  );
		gallery._arrow_box.set_item_size  ( gallery._thumb_size [ 'w' ] , gallery._thumb_size [ 'h' ] );

		gallery._arrow_box.cbacks [ 'item-clicked' ] = function ( sb, elem ) { 
			gallery._display ( elem._pos -1 ); 
		};

		count = gallery._items.count ();
		items = [];

		if ( ! pos ) pos = 0;
		gallery._items.iterate ( function ( item, k )
		{
			item.get_img_html ( gallery._thumb_size [ 'w' ], gallery._thumb_size [ 'h' ], 'gallery_thumbnail', function ( html )
			{
				count --;
				//gallery._arrow_box.add_item ( html );
				items [ k ] = html;

				if ( count == 0 ) 
				{
					items.iterate ( function ( v, k )
					{
						gallery._arrow_box.add_item ( v );
					} );
					gallery._arrow_box.render ( gallery._elems [ 'arrbox' ] );
					// gallery._arrow_box.
				}
			} );

		} );
	};
	// }}}
	//{{{_go_fullscreen ()
	gallery._go_fullscreen = function ()
	{
		var _real_curr_pos = gallery._curr_pos;

		if ( gallery._in_fullscreen )
		{
			liwe.dom.remove_element ( gallery._in_fullscreen );
			gallery._in_fullscreen = false;
			gallery.render ( gallery._real_dest_div );
		} else {
			var w, h, size = liwe.dom.get_body_size ();
			var e = liwe.dom.create_element ( 'div' );

			e.id = gallery.name + "_fullscreen";
			e.style.width  = size [ 0 ] + "px";
			e.style.height = size [ 1 ] + "px";
			e.style.zIndex = '10000';

			gallery._real_dest_div = gallery._dest_div;
			$( gallery._dest_div, "" );

			setTimeout ( function () {

				gallery.render ( e );

				gallery._in_fullscreen = e;
			}, 100 );
			window.scrollTo ( 0,0 );
		}

		// gallery._display ( _real_curr_pos );
	};
	//}}}
	//{{{_show_video ()
	gallery._show_video = function ()
	{
		var size = liwe.dom.get_size ( gallery._elems [ 'gallery' ] );
		var item = gallery._curr_item;
		var dest = gallery._elems [ 'video' ];

		var obj = document.createElement ( "object" );
		obj.setAttribute ( "type","application/x-shockwave-flash" );
		obj.setAttribute ( "width", size [ 0 ] );
		obj.setAttribute ( "height",size [ 1 ] );
		obj.setAttribute ( "wmode", "opaque" );
		obj.setAttribute ( "data","http://www.youtube.com/v/" + item [ 'youtube' ] + "&autoplay=1" );

		var par1 = document.createElement ( "param" );
		par1.setAttribute ( "name", "movie" );
		par1.setAttribute ( "value", "http://www.youtube.com/v/" + item [ 'youtube' ] + "&autoplay=1" );
		par1.setAttribute ( "wmode", "opaque" );
		obj.appendChild ( par1 );

		// gallery.controls_hide ();
		gallery.thumbs_hide ();
		gallery.video_hide ();

		$( gallery._elems [ 'footer' ] ).style.display = 'none';

		dest.appendChild ( obj );
	};
	//}}}
	//}}}

	return gallery;
};

// {{{ WWL.gallery._img_next ( name )
WWL.gallery._img_next = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal.img_next ();
};
// }}}
// {{{ WWL.gallery._img_prev ( name )
WWL.gallery._img_prev = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal.img_prev ();
};
// }}}
// {{{ WWL.gallery._evt_hover ( name )
WWL.gallery._evt_hover = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal.evt_hover ();
};
// }}}
// {{{ WWL.gallery._evt_out ( name )
WWL.gallery._evt_out = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal.evt_out ();
};
// }}}
// {{{ WWL.gallery._block_timeout ( name )
WWL.gallery._block_timeout = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal.block_timeout ();
};
// }}}
// {{{ WWL.gallery._fullscreen ( name )
WWL.gallery._fullscreen = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal._go_fullscreen ();
};
// }}}
// {{{ WWL.gallery._video ( name )
WWL.gallery._video = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );

	gal._show_video ();
};
// }}}
// {{{ WWL.gallery._thumbs_show ( name )
WWL.gallery._thumbs_show = function ( name )
{
	var gal = WWL.gallery.get_instance ( name );
	if ( gal._thumbs_shown )
		gal.thumbs_hide ();
	else
		gal.thumbs_show ();

	
};
// }}}
//{{{WWL.gallery.get_instance = function ( instance_name )
WWL.gallery.get_instance = function ( instance_name )
{
	return WWL.get_instance ( "gallery", instance_name );
};
//}}}
//{{{WWL.gallery.display_item = function ( name, id )
WWL.gallery.display_item = function ( name, id )
{
	var g = WWL.gallery.get_instance ( name );
	g.display_item ( id );	
};
//}}}

